package me.kagami.autotesthelper;


import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;

/**
 * 插件学习途径，可以去论坛查找相关帖子
 * https://intellij-support.jetbrains.com/hc/en-us/community/topics/200366979-IntelliJ-IDEA-Open-API-and-Plugin-Development
 */


public class AutoTestHelpMain extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent event) {
        AutoTestHelpDialog autoTestHelpDialog = new AutoTestHelpDialog();
        autoTestHelpDialog.setLocationRelativeTo(autoTestHelpDialog);
        autoTestHelpDialog.pack();
        autoTestHelpDialog.setVisible(true);
        final Project project = event.getProject();

        final AutoTestHelpDto autoTestHelpDto = autoTestHelpDialog.getAutoTestHelpDto();

        GenerateMapHandler generateMapHandler = new GenerateMapHandler(project);
        String result = "";
        try {
            if (StringUtils.isNotEmpty(autoTestHelpDto.getFunctionName())) {
                if (autoTestHelpDto.isYaml()) {
                    result = MapToJsonYamlUtil.jsonConverToYaml(MapToJsonYamlUtil.mapToJson(generateMapHandler.getMapFromFunction(autoTestHelpDto.getClassName(), autoTestHelpDto.getFunctionName())));
                }
                if (autoTestHelpDto.isJson()) {
                    result = MapToJsonYamlUtil.mapToJson(generateMapHandler.getMapFromFunction(autoTestHelpDto.getClassName(), autoTestHelpDto.getFunctionName()));
                }
            } else {
                if (autoTestHelpDto.isYaml()) {
                    result = MapToJsonYamlUtil.jsonConverToYaml(MapToJsonYamlUtil.mapToJson(generateMapHandler.getMap(autoTestHelpDto.getClassName())));
                }
                if (autoTestHelpDto.isJson()) {
                    result = MapToJsonYamlUtil.mapToJson(generateMapHandler.getMap(autoTestHelpDto.getClassName()));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = "解析过程发生异常";
        }
        if (StringUtils.isNotEmpty(result)) {
            ShowResultDialog showResultDialog = new ShowResultDialog();
            showResultDialog.getTextArea1().setText(result);
            showResultDialog.setLocationRelativeTo(showResultDialog);
            showResultDialog.pack();
            showResultDialog.setVisible(true);
        }

    }
}